﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace Speedy
{
    public partial class frmCihazAyarlari : XtraForm
    {
        public frmCihazAyarlari()
        {
            InitializeComponent();
        }

        private void frmCihazAyarlari_Load(object sender, EventArgs e)
        {

        }

        bool CihazKaydet()
        {
            try
            {
                using (dbDevices device = new dbDevices())
                {
                    device.id.SetValue(txtId.Text);
                    device.name.SetValue(txtName.Text);
                    device.device_code.SetValue("");
                    device.license_code.SetValue("");
                    device.Save();
                }
                return true;
            }
            catch (Exception ex)
            {
                Logging.GetLog.ErrorLog(ex);
                return false;
            }
        }

        void AyarKaydet()
        {
            dbDeviceParameters prm = new dbDeviceParameters();
            prm.device_id.SetValue(txtId.Text);
            prm.Remove();

            for (int i = 1; i < vGridControl1.Rows.Count; i++)
            {
                if (vGridControl1.Rows[i].Properties.Value != null)
                {
                    using (prm = new dbDeviceParameters())
                    {
                        prm.device_id.SetValue(txtId.Text);
                        prm.paramCode.SetValue(vGridControl1.Rows[i].Properties.FieldName);
                        prm.paramValue.SetValue(vGridControl1.Rows[i].Properties.Value.ToString());
                        prm.Save();
                    }
                }
            }
        }


        void temizle()
        {
            for (int i = 1; i < vGridControl1.Rows.Count; i++)
            {
                if (vGridControl1.Rows[i].Properties.Value != null)
                {
                    vGridControl1.Rows[i].Properties.Value = string.Empty;
                }
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (CihazKaydet())
            {
                AyarKaydet();
                this.Close();
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            temizle();
        }

        private void txtId_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmListe lst = new frmListe();
            lst.ListeTipi = ListeTipleri.Terminal;
            lst.Filtre = string.Empty;
            if (lst.ShowDialog() == DialogResult.OK)
                if (lst.Value.Length > 0)
                {
                    txtId.Text = lst.Value[0];
                    txtName.Text = lst.Value[1];
                    //txtMachine.Text = lst.Value[2];
                    //txtLisans.Text = lst.Value[3];
                }

            dbDeviceParameters prm = new dbDeviceParameters();
            prm.device_id.SetValue(txtId.Text);
            DataTable dt = prm.AllData();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int v = 0; v < vGridControl1.Rows.Count; v++)
                    if (dt.Rows[i]["PARAMETER_CODE"].ToString() == vGridControl1.Rows[v].Properties.FieldName)
                    {
                        switch (vGridControl1.Rows[v].Properties.UnboundType)
                        {
                            case DevExpress.Data.UnboundColumnType.Boolean:
                                vGridControl1.Rows[v].Properties.Value = Convert.ToBoolean(dt.Rows[i]["PARAMETER_VALUE"]);
                                break;
                            case DevExpress.Data.UnboundColumnType.DateTime:
                                vGridControl1.Rows[v].Properties.Value = Convert.ToDateTime(dt.Rows[i]["PARAMETER_VALUE"]);
                                break;
                            case DevExpress.Data.UnboundColumnType.Decimal:
                                vGridControl1.Rows[v].Properties.Value = Convert.ToDecimal(dt.Rows[i]["PARAMETER_VALUE"]);
                                break;
                            case DevExpress.Data.UnboundColumnType.Integer:
                                vGridControl1.Rows[v].Properties.Value = Convert.ToInt32(dt.Rows[i]["PARAMETER_VALUE"]);
                                break;
                            case DevExpress.Data.UnboundColumnType.String:
                                vGridControl1.Rows[v].Properties.Value = Convert.ToString(dt.Rows[i]["PARAMETER_VALUE"]);
                                break;
                            default:
                                vGridControl1.Rows[v].Properties.Value = dt.Rows[i]["PARAMETER_VALUE"];
                                break;
                        }
                        break;
                    }
            }

        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            dbDeviceParameters prm = new dbDeviceParameters();
            prm.device_id.SetValue(txtId.Text);
            prm.Remove();

            dbDevices dev = new dbDevices();
            dev.id.SetValue(txtId.Text);
            dev.Remove();

            this.Close();
        }
    }
}
